package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.adapter.CompanyInfoAdapter;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CompanyListActivity extends HttpRequestActivity {

    private static final String TAG = "CompanyListActivity";
    private ActionBar mActionBar = null;

    private RecyclerView mCompanyListView = null;

    //资源关键字
    CompanyInfoAdapter mAdapter = null;
    List<CompanyInfo> mCompanyList = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(CompanyListActivity.class);
        setContentView(R.layout.activity_company_list);
        mCompanyList = new ArrayList<>();
        mAdapter = new CompanyInfoAdapter();

        mCompanyListView = findViewById(R.id.rv_company_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getBaseContext());
        mCompanyListView.setLayoutManager(layoutManager);
        mCompanyListView.setAdapter(mAdapter);

        DividerItemDecoration divider = new DividerItemDecoration(getBaseContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.divider));
        mCompanyListView.addItemDecoration(divider);

        mActionBar = findViewById(R.id.actionBarCompanyList);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeSearchCompanyInfo();
    }

    private void refreshCompanyView() {
        mAdapter.clearData();
        mAdapter.appendData(mCompanyList);
        mAdapter.notifyDataSetChanged();
    }

    private void executeSearchCompanyInfo(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_COMPANY,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_USER_COMPANY);
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    JSONArray data = res.getJSONArray("data");
                    Type type = new TypeToken<List<CompanyInfo>>() {}.getType();
                    mCompanyList = gson.fromJson(data.toString(), type);
                    refreshCompanyView();
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
            ToastUtils.showLongToast(this, "获取公司信息失败");
        }
        else{
            /**/
        }
    }
}